﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Web;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.DAL.Gateway;

/// <summary>
/// Summary description for UserDAL
/// </summary>
public class UserDAL : DbGateway
{
    public void CreateUser(UserObj userObj)
    {
        try
        {
            ConnectionObj.Open();
            string insert = String.Format("Insert Into User Values(@UserName,@PassWord,@Role_Id,@EmployeeCode)");
            CommandObj.CommandText = insert;
            CommandObj.Parameters.Clear();
            CommandObj.Parameters.Add("@UserName", SqlDbType.VarChar);
            CommandObj.Parameters["@UserName"].Value = userObj.UserName;
            CommandObj.Parameters.Add("@PassWord", SqlDbType.VarChar);
            CommandObj.Parameters["@PassWord"].Value = userObj.PassWord;
            CommandObj.Parameters.Add("@Role_Id", SqlDbType.VarChar);
            CommandObj.Parameters["@Role_Id"].Value = userObj.Role.RoleId;
            CommandObj.Parameters.Add("@EmployeeCode", SqlDbType.VarChar);
            CommandObj.Parameters["@EmployeeCode"].Value = userObj.EmployeeCode;
            CommandObj.ExecuteNonQuery();


        }
        catch (Exception)
        {
            throw new Exception("Database Error");
        }
        finally
        {
            if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
            {
                ConnectionObj.Close();
            }
        }


    }
    public void UpdateUser(UserObj userObj)
    {
        try
        {

            ConnectionObj.Open();
            string update = String.Format("Update User SET UserName={0} and PassWord='{1}' and Role_Id='{2}' Where UserName='{3}' and PassWord='{4}'", userObj.UserName, userObj.PassWord, userObj.Role.RoleId, userObj.UserName, userObj.UserName);
            CommandObj.ExecuteNonQuery();


        }
        catch (Exception)
        {
            throw new Exception("Database Error");
        }
        finally
        {
            if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
            {
                ConnectionObj.Close();
            }
        }
    }
    public UserObj CheckUser(UserObj userObj)
    {
        UserObj auserObj = null;
        Role role = new Role();

        try
        {
            ConnectionObj.Open();
            string select = String.Format("SELECT * FROM Users Where UserName='{0}' and Password='{1}'", userObj.UserName,
                                         userObj.PassWord);
            CommandObj.CommandText = select;
            SqlDataReader reader = CommandObj.ExecuteReader();
            while (reader.Read())
            {
                auserObj=new UserObj();
                auserObj.UserName = reader[1].ToString();
                auserObj.PassWord = reader[2].ToString();
                role.RoleId = Convert.ToInt32(reader[3]);
                auserObj.Role = role;
            }


        }
        catch (Exception)
        {
            throw new Exception("Database Error");
        }
        finally
        {
            if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
            {
                ConnectionObj.Close();
            }
        }
        return auserObj;
    }
}